#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
将citing_articles.txt文件转换为CSV格式
格式：文章标题,编号,URL -> CSV文件
"""

import csv
import os

def convert_txt_to_csv():
    """将txt文件转换为CSV格式"""
    
    input_file = "data/citing_articles.txt"
    output_file = "data/citing_articles.csv"
    
    if not os.path.exists(input_file):
        print(f"错误：找不到输入文件 {input_file}")
        return
    
    print("正在转换文件格式...")
    
    # 统计信息
    total_lines = 0
    processed_lines = 0
    error_lines = 0
    
    with open(input_file, 'r', encoding='utf-8') as txt_file, \
         open(output_file, 'w', encoding='utf-8', newline='') as csv_file:
        
        # 创建CSV写入器
        csv_writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)
        
        # 写入表头
        csv_writer.writerow(['文章标题', '编号', 'URL'])
        
        # 逐行处理
        for line_num, line in enumerate(txt_file, 1):
            total_lines += 1
            line = line.strip()
            
            if not line:
                continue
            
            # 分割数据 - 只分割前两个逗号，因为URL中可能包含逗号
            parts = line.split(',', 2)
            
            if len(parts) != 3:
                print(f"警告：第{line_num}行格式不正确，跳过: {line[:100]}...")
                error_lines += 1
                continue
            
            article_title, number, url = parts
            
            # 写入CSV行
            csv_writer.writerow([article_title.strip(), number.strip(), url.strip()])
            processed_lines += 1
            
            # 显示进度
            if processed_lines % 1000 == 0:
                print(f"已处理 {processed_lines} 行...")
    
    print(f"\n✅ 转换完成！")
    print(f"📁 输入文件: {input_file}")
    print(f"📁 输出文件: {output_file}")
    print(f"📊 处理统计:")
    print(f"   - 总行数: {total_lines}")
    print(f"   - 成功处理: {processed_lines}")
    print(f"   - 错误行数: {error_lines}")
    
    # 获取输出文件大小
    if os.path.exists(output_file):
        file_size = os.path.getsize(output_file)
        print(f"   - 输出文件大小: {file_size:,} 字节")
    
    # 显示CSV文件的前几行作为示例
    print(f"\n📋 CSV文件预览:")
    try:
        with open(output_file, 'r', encoding='utf-8') as csv_file:
            csv_reader = csv.reader(csv_file)
            for i, row in enumerate(csv_reader):
                if i < 5:  # 显示前5行（包括表头）
                    # 截断过长的内容以便显示
                    display_row = []
                    for cell in row:
                        if len(cell) > 50:
                            display_row.append(cell[:47] + "...")
                        else:
                            display_row.append(cell)
                    print(f"   {i+1:2d}. {display_row}")
                else:
                    break
    except Exception as e:
        print(f"预览CSV文件时出错: {e}")

if __name__ == "__main__":
    convert_txt_to_csv()